package com.dbq.fast.business.me.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.me.api.RewardApi;
import com.dbq.fast.common.net.RemoteApiCallBack;
import com.dbq.fast.util.ProgressDialogUtil;

/**
 * Created by yuanchen on 2018/5/4.
 */

public class BindWxFragment extends DialogFragment {

    public static int sIsBindWx = 0;

    public static BindWxFragment newInstance(){
        BindWxFragment fragment = new BindWxFragment();
        return fragment;
    }

    public static void showBindWxDialog(FragmentManager fragmentManager){
        FragmentTransaction ft = fragmentManager.beginTransaction();
        BindWxFragment prev = (BindWxFragment) fragmentManager.findFragmentByTag("FREE_DAYS");
        if (prev != null) {
            if (prev.isVisible()) {
                prev.dismiss();
            }
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        BindWxFragment adDialogFragment = BindWxFragment.newInstance();
        try {
            adDialogFragment.show(ft, "BIND_WX");
        }catch (Exception e){}
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(false);// 点击外部不消失，点击X和返回键消失
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x4c000000));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getDialog().getWindow().setDimAmount(0.45f);
        }
        getDialog().getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.bind_wx_fragment, null);

        EditText editWx = view.findViewById(R.id.edit_wx_account);
        view.findViewById(R.id.bind_wx_btn).setOnClickListener(view1 -> {
            String content = editWx.getText().toString().trim();
            if("".equals(content)){
                Toast.makeText(App.getContext(), getString(R.string.tip_input_account_is_null), Toast.LENGTH_SHORT).show();
            }else {
                YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_BIND_WX);
                bindWx(content);
            }
        });

        return view;
    }

    private void bindWx(String wxAccount) {
        ProgressDialogUtil.showProgress(getActivity(), "", getString(R.string.commiting));
        RewardApi.bindWx(wxAccount, new RemoteApiCallBack() {
            @Override
            public void onFail(int status, String message) {
                if(isResumed()){
                    Toast.makeText(App.getContext(), getString(R.string.tip_retry_network_bad), Toast.LENGTH_SHORT).show();
                    ProgressDialogUtil.hideProgress();
                }
            }

            @Override
            public void onSuccess(Object o) {
                sIsBindWx = 1;
                if (isResumed()) {
                    Toast.makeText(App.getContext(), getString(R.string.bind_wx_success), Toast.LENGTH_SHORT).show();
                    ProgressDialogUtil.hideProgress();
                    dismiss();
                }
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }

    @Override
    public void onDestroy() {
        if(isResumed()) {
            ProgressDialogUtil.hideProgress();
        }
        super.onDestroy();
    }
}
